package com.wrh.draw.legend;

import java.awt.*;

/**
 * @author wrh
 */
public class HexColor {

    private final String hexColor;

    public HexColor(int r, int g, int b, int a) {
        this.hexColor = HexColorUtil.convertRgbaToHex(r, g, b, a);
    }

    public HexColor(int r, int g, int b) {
        this.hexColor = HexColorUtil.convertRgbToHex(r, g, b);
    }

    public HexColor(String hexColor) {
        HexColorUtil.testValidateHexadecimalColor(hexColor);
        if (hexColor.length() == 7) {
            this.hexColor = hexColor + "FF";
        } else {
            this.hexColor = hexColor;
        }
    }

    public Color getColor() {
        return HexColorUtil.convertHexToColor(this.hexColor);
    }

    public String getHexColor() {
        return hexColor;
    }

    public int getRed() {
        String r = this.hexColor.substring(1, 3);
        return HexColorUtil.get10HexInt(r);
    }

    public int getGreen() {
        String g = this.hexColor.substring(3, 5);
        return HexColorUtil.get10HexInt(g);
    }

    public int getBlue() {
        String b = this.hexColor.substring(5, 7);
        return HexColorUtil.get10HexInt(b);
    }

    public int getAlpha() {
        String a = this.hexColor.substring(7, 9);
        return HexColorUtil.get10HexInt(a);
    }

    @Override
    public String toString() {
        return getHexColor();
    }
}
